/*
 * vmcopy.c
 */

#include <stdio.h>
#include "vmbdoor.h"

/*
 * main
 */
int main (int argc, char **argv)
{
	uint32 maxlen, length, command, pref;

#ifdef _MSDOS
	char buf[4096];
#else
	char buf[VMCLIP_MAX_LENGTH + 1];
#endif	/* _MSDOS */

	/* are we running in VMware ? 	*/
	if (VMGetVersion() == 0) {
		printf("Not running inside VMware.\n");
		return 0;
	}

	/* is copy & paste enabled ?	*/
	command = VMCMD_GET_SETTING;
	VMBackDoor(&pref, NULL, &command, NULL);
	
	if (!(pref & VMPREF_COPY_PASTE)) {
		printf("Copy & paste is disabled.\n");
		return 0;
	}
	
	maxlen = sizeof(buf) - 1;
	length = 0;

	while ((buf[length] = getchar()) != EOF) {
		if (buf[length] == '\n') {
			maxlen--;
		}
		if (++length >= maxlen) {
			break;
		}
	}

	if (length) {
		uint32 i = 0;

		buf[length++] = '\0';
		
		/* send clipboard data length	*/
		command = VMCMD_SET_CLIPLEN;
		VMBackDoor(NULL, &length, &command, NULL);

		while (i < length) {
			command = VMCMD_SET_CLIPDATA;
			VMBackDoor(NULL, (uint32 *)(buf + i), &command, NULL);

			i += 4;
		}
	}
	return 1;
}
/* End of File */
